/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cs425proj.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author rakesh reddy
 */
@WebServlet(name = "newDiscussionServlet", urlPatterns = {"/newDiscussionServlet"})
public class newDiscussionServlet extends HttpServlet {
 private Connection con ;
     private Statement stm,stm1,stm2,stm3;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        int movie_id=0;
        int theater_id=0,vis=0,mem_id=0;
                int p_total=0;
        
        try {
            HttpSession session= request.getSession();
            String member=(String)session.getAttribute("name");
             this.startConnection();
             String title = request.getParameter("title");
             String movie_name = request.getParameter("movieselect");
             String theater_name = request.getParameter("theaterselect");
             String content = request.getParameter("content");
             String query1 ="select movie_id from movie where name='"+movie_name+"'";
             String query2 ="select theater_id from theater where theater_name='"+theater_name+"'";
             String query3 ="select member_id from member where username='"+member+"'";
        //     String query5 ="update member set total_points = (total_points + 2) where username='"+member+"'"; //adds 2 points to user when he creates a discussion
             stm1=con.createStatement();
             stm2=con.createStatement();
             stm3=con.createStatement();
             
             ResultSet rs1 = stm1.executeQuery(query1);
             while(rs1.next())
             {
                 movie_id=rs1.getInt(1);
             }
             ResultSet rs2 = stm2.executeQuery(query2);
             while(rs2.next())
             {
                 theater_id=rs2.getInt(1);
             }
              ResultSet rs3 = stm3.executeQuery(query3);
             while(rs3.next())
             {
                 mem_id=rs3.getInt(1);
             }
             String qry = "insert into discussion values(seq_discussion_id.nextval ,?,?,?,?,?,?)";
             PreparedStatement pstmt = con.prepareStatement(qry);
          //  PreparedStatement pstmt1 = con.prepareStatement(query5);
            pstmt.setInt(1,mem_id);
            pstmt.setString(2,content);
            pstmt.setInt(3, movie_id);
            pstmt.setInt(4, theater_id);
            pstmt.setString(5, title);
            pstmt.setInt(6,vis);
            pstmt.executeUpdate(); 
            /*
             * TODO output your page here. You may use following sample code.
             */
           
            
          //  stm2.executeQuery(query5);
            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet newDiscussionServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet newDiscussionServlet at " + request.getContextPath() + "</h1>");
            out.println("<a href=\"homepage.jsp\">back to home</a>");
            
           // p_total= p_total+ 2;
          //  pstmt1.setInt(9,p_total);
         //   pstmt1.executeUpdate(); 
            
           
            out.println("</body>");
            out.println("</html>");
            this.closeConnection();
        }catch(SQLException e){e.printStackTrace();}   finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
     public void startConnection(){
        
       String connStr ="jdbc:oracle:thin:@fourier.cs.iit.edu:1521:orcl";
       
       
       
           try{
               //load the drives
            Class.forName("oracle.jdbc.driver.OracleDriver");

            //initialize the connection variable
               con = DriverManager.getConnection(connStr, "rjairaj", "cs425dbo");
                     
                      
             }catch(SQLException e){e.printStackTrace();}
              catch(ClassNotFoundException cn){cn.printStackTrace();}
           
           }
    
    public void closeConnection(){
           try{
            con.commit();
            con.close();
    
           }catch(SQLException e){e.printStackTrace();}
    }
}
